package com.hula.onion.book.server.spider.platforms.renrendai;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.model.AccountInfo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:49
 *
 * @author shan
 * @version 1.0
 */
@Component
public class RenrenDaiSpiderHelper {

    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMO0o8vYsqInbD/8uraIdWqP8Ycc7KQuLS7w0VbCWocyMRYu582LwzycBOPvbbEKt2feqpUKQ+F3peq+HQnI6gL9d66l0ZG3KjflZTQJ8M847USfUNGVbAi3PJG/NiwQHddUUudmjIEAXwadelp/g+/p97YcBAz8caQDcEyI0AjQIDAQAB";

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) throws Exception {

        Map<String, Object> map = new HashMap<>(6);

        map.put("j_username", form.getUserName());
        map.put("j_password", form.getPassword());
        map.put("j_code", form.getTicket());
        map.put("rememberme", "on");
        map.put("targetUrl", "");
        map.put("returnUrl", "");

        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public AccountInfo parseAccountBalance(String html) {

        AccountInfo accountInfo = new AccountInfo();

        String avaliableBalance =  JSONObject.parseObject(html).getString("avaliableBalance");
        accountInfo.setAmount(new BigDecimal(avaliableBalance));

        return accountInfo;
    }


}
